<template>
  <div class="leave-statistics-container">
    <el-card class="page-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-icon class="header-icon"><DataAnalysis /></el-icon>
            <span class="header-title">假期统计</span>
          </div>
          <div class="header-right">
            <el-button type="success" @click="exportData" :loading="exportLoading">
              <el-icon><Download /></el-icon>
              导出Excel
            </el-button>
          </div>
        </div>
      </template>
      
      <div v-if="statistics" class="statistics-content">
        <div class="statistics-grid">
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">年假</div>
              <div class="stat-value primary">{{ statistics.annualCount || 0 }}</div>
            </div>
          </el-card>
          
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">病假</div>
              <div class="stat-value warning">{{ statistics.sickCount || 0 }}</div>
            </div>
          </el-card>
          
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">事假</div>
              <div class="stat-value info">{{ statistics.personalCount || 0 }}</div>
            </div>
          </el-card>
          
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">调休</div>
              <div class="stat-value success">{{ statistics.adjustmentCount || 0 }}</div>
            </div>
          </el-card>
        </div>
        
        <el-divider />
        
        <div class="status-grid">
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">待审批</div>
              <div class="stat-value warning">{{ statistics.pendingCount || 0 }}</div>
            </div>
          </el-card>
          
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">已通过</div>
              <div class="stat-value success">{{ statistics.approvedCount || 0 }}</div>
            </div>
          </el-card>
          
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">已拒绝</div>
              <div class="stat-value danger">{{ statistics.rejectedCount || 0 }}</div>
            </div>
          </el-card>
          
          <el-card class="stat-card" shadow="hover">
            <div class="stat-item">
              <div class="stat-label">总计</div>
              <div class="stat-value highlight">{{ statistics.totalCount || 0 }}</div>
            </div>
          </el-card>
        </div>
      </div>
      
      <div v-else class="loading-state">
        <el-empty description="加载中..." />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import request from '../utils/request'
import { ElMessage } from 'element-plus'
import { DataAnalysis, Download } from '@element-plus/icons-vue'

export default {
  name: 'LeaveStatistics',
  components: {
    DataAnalysis,
    Download
  },
  setup() {
    const statistics = ref(null)
    const exportLoading = ref(false)
    
    // 导出数据
    const exportData = async () => {
      exportLoading.value = true
      try {
        const token = localStorage.getItem('token')
        const url = '/api/leave/export'
        
        const response = await fetch(url, {
          method: 'GET',
          headers: {
            'Authorization': `Bearer ${token}`
          }
        })
        
        if (!response.ok) {
          throw new Error('导出失败')
        }
        
        const blob = await response.blob()
        const downloadUrl = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = downloadUrl
        link.download = `请假记录_${new Date().toISOString().slice(0, 7)}.xlsx`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(downloadUrl)
        
        ElMessage.success('导出成功')
      } catch (error) {
        ElMessage.error('导出失败：' + (error.message || '未知错误'))
      } finally {
        exportLoading.value = false
      }
    }
    
    const loadStatistics = async () => {
      try {
        const res = await request.get('/leave/statistics')
        statistics.value = res.data || {}
      } catch (error) {
        console.error('加载统计数据失败:', error)
        if (error.message && error.message.includes('404')) {
          ElMessage.error('请假服务未启动或未注册，请检查leave-service是否正常运行')
        } else if (error.message && error.message.includes('403')) {
          ElMessage.error('无权限访问，仅HR角色可以查看假期统计')
        } else {
          ElMessage.error(error.message || '加载统计数据失败')
        }
      }
    }
    
    onMounted(() => {
      loadStatistics()
    })
    
    return {
      statistics,
      exportLoading,
      exportData
    }
  }
}
</script>

<style scoped>
.leave-statistics-container {
  width: 100%;
  height: 100%;
}

.page-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 24px;
  color: #409eff;
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
}

.statistics-content {
  padding: 20px 0;
}

.statistics-grid,
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 12px;
  border: none;
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.stat-item {
  text-align: center;
  padding: 20px;
}

.stat-label {
  font-size: 14px;
  color: #8c8c8c;
  margin-bottom: 12px;
}

.stat-value {
  font-size: 36px;
  font-weight: 700;
}

.stat-value.primary {
  color: #409eff;
}

.stat-value.warning {
  color: #e6a23c;
}

.stat-value.info {
  color: #909399;
}

.stat-value.success {
  color: #67c23a;
}

.stat-value.danger {
  color: #f56c6c;
}

.stat-value.highlight {
  color: #409eff;
  font-size: 42px;
}

.loading-state {
  padding: 60px 0;
}
</style>

